/*
 * FOF组合优化模块
 */
require('!style!css!../css/combination.css');
var combinationHtml = require('!html!../html/combination.html');
var common = require('./common.js');
var page = require('./page.js');
var combinationGraph = require('./combinationGraph.js');
var combinationVue;
var combination = {
    option: {},
    myChart: null,
    param: common.staticData.configOrCombinationParam,
    init: function (option) {
        this.option = $.extend(this.option, option);
        page.setPage(6);
        common.initNav(4);
        $('.step').hide();
        $('#combination').html(combinationHtml).show();
        this.initModel();
        this.initFund();
        this.initCallback();
    },
    initFund: function () {
        page.initSelectedParams(6);
        combinationVue = new Vue({
            el: '#combination',
            data: {
                fundList: window['fundCheckList']
            },
            methods: {
                startCalculate: function () {
                    combination.next();
                }
            }
        });
        $('.config-model').on('change', '.table-bordered input[type="radio"]', function () {
            var val = $(this).val();
            $(this).parent().parent().parent().next().find('input').val(val);
        });
        $('.config-model').on('change', '.table-bordered input[type="range"]', function () {
            var val = $(this).val();
            $(this).parent().find('input[type="text"]').val(val * 10);
        });
    },
    next: function () {
        console.log('combination startCalculate click...');
        var model = $('input[name="combinationModelSelect"]:checked').val();
        if (!model) {
            alert('请选择模型');
            return false;
        }
        var isNull = false;
        var upDownParams = [];
        $('#combination .strategy-list tr').each(function () {
            var up = $(this).find('input').eq(0).val();
            up = $.trim(up);
            var down = $(this).find('input').eq(1).val();
            down = $.trim(down);
            if (!up || !down) {
                isNull = true;
            }
            upDownParams.push({
                id: $(this).attr('data-id'),
                up: up,
                down: down,
                assetStrategyId: parseInt($(this).attr('data-assetStrategyId'), 10)
            });
        });
        if (isNull) {
            alert('请填写上限/下限值');
            return false;
        }
        var paramJson = {};
        paramJson.modelId = model;
        paramJson.calType = 2;
        var viewpointParam = [];
        var weightParam = [];
        var param = $('#combination .config-model');
        if (model == 2) {
            param.find('.viewpoint').each(function () {
                var val = $(this).val();
                if (!val) {
                    isNull = true;
                }
                viewpointParam.push({
                    viewpoint: val / 100,
                    confidence: $(this).parent().parent().next().find('input[type="text"]').val() / 100
                });
            });
            if (isNull) {
                alert('请选择观点');
                return false;
            }
        } else if (model == 4) {
            var isMax = true;
            var weight = 0;
            param.find('input[type="text"]').each(function () {
                var val = $(this).val();
                if (!val) {
                    isNull = true;
                } else {
                    weight += parseFloat(val);
                }
                weightParam.push({
                    weight: val
                });
            });
            console.log('weight:' + weight);
            if (weight < 99 || weight > 100) {
                isMax = false;
            }
            if (isNull) {
                alert('请填写权重值');
                return false;
            }
            if (!isMax) {
                alert('权重值错误');
                return false;
            }
        }
        var childParams = [];
        $('#combination .model-param').each(function () {
            var child = $(this).find('input:checked').val();
            childParams.push({value: child});
        });
        paramJson.modelParams = childParams;
        paramJson.upDownParams = upDownParams;
        paramJson.viewpointParams = viewpointParam;
        paramJson.weightParam = weightParam;
        console.log(JSON.stringify(paramJson));
        combinationGraph.init(paramJson);
    },
    initCallback: function () {

    },
    getConfigParamHtml: function (data, extra) {
        $.extend(data, extra);
        var html = '';
        html += '<div class="model-param">';
        html += '   <p class="title">' + data.num + '、' + data.title + '</p>';
        var radioName = '';
        $.each(data.list, function (k, v) {
            radioName = v.name || radioName;
            html += '   <div class="radio">';
            html += '        <label>';
            html += '           <input type="radio" name="' + radioName + '" value="' + v.value + '" ' + (v.checked ? 'checked' : '') + '>' + v.text;
            html += '       </label>';
            html += '   </div>';
        });
        html += '</div>';
        return html;
    },
    initModel: function () {
        $('input[name="combinationModelSelect"]').on('change', function () {
            var val = $(this).val();
            var configHtml = '';
            $('.strategy-list').show();
            if (val == 1) {
                configHtml += combination.getConfigParamHtml(combination.param.iteration, {num: 1});
                configHtml += combination.getConfigParamHtml(combination.param.backLength, {num: 2});
            } else if (val == 2) {
                configHtml += combination.getConfigParamHtml(combination.param.backLength, {num: 1});
                configHtml += $('#combinationBL').html();
            } else if (val == 3) {
                configHtml += combination.getConfigParamHtml(combination.param.backLength, {num: 1});
            } else if (val == 4) {
                configHtml += $('#combinationCustomWeight').html();
                $('.strategy-list').hide();
            }
            $('.config-model').html(configHtml);
            if (val == 4) {
                var percent = '';
                var size = combinationVue.fundList.length;
                if (size) {
                    if (100 % size == 0) {
                        percent = 100 / size;
                    } else {
                        //percent = parseFloat('' + 100 / size).toFixed(2);
                        percent = Math.floor(100 / size * 100) / 100;
                    }
                }
                $('.config-model').find('input').val(percent);
            }
        });
    }
};
for (var key in combination) {
    if (key) {
        exports[key] = combination[key];
    }
}